package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Service.AreaService;
import com.realtyManage.springboot.Service.IUserService;
import com.realtyManage.springboot.Service.IUserTaskService;
import com.realtyManage.springboot.Service.impl.UserTaskServiceImpl;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class UserTaskController {

    @Autowired
    private IUserTaskService userTaskService;

    @Autowired
    private IUserService userService;


    /**
     * 设置等级时间
     * @param customerLevelTime
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addLevelTime",method = RequestMethod.POST)
    private String addLevelTime(CustomerLevelTime customerLevelTime){
        Integer code = -1;
         if (customerLevelTime.getId() == ' '){    //前端莫名奇妙传这玩意
             customerLevelTime.setId(null);
         }
         if (userTaskService.insertLevelTime(customerLevelTime).getId() != null){
            code = 1;
         }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 删除客户等级时间设置
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteLevelTime",method = RequestMethod.POST)
    private String deleteLevelTime(HttpServletRequest request){
        String ids = request.getParameter("ids");
        Integer code = -1;
        if (userTaskService.deleteLevelTime(ids) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    @ResponseBody
    @RequestMapping(value = "/getLevelTime",method = RequestMethod.GET)
    private String getLevelTime(RegEntity regEntity) {
        Page<CustomerLevelTime> areaList = userTaskService.findPagedByWhereClause(regEntity);
        return JSON.toJSONString(areaList);
    }


    /**
     * 我的今日客户跟踪提醒
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/myCustomerRemind",method = RequestMethod.GET)
    private String myCustomerRemind(BaseEntity entity, HttpServletRequest request) {
        List<CustomerRegistration> lst = userTaskService.remindList(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        return JSON.toJSONString(lst);
    }


    /**
     * 团队客户提醒
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/teamCustomerRemind",method = RequestMethod.GET)
    private String teamCustomerRemind(BaseEntity entity, HttpServletRequest request) {
        List<CustomerRegistration> lst = userTaskService.teamCustomerRemind();
        return JSON.toJSONString(lst);
    }

    /**
     * 我的月度工作任务
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMyTask",method = RequestMethod.GET)
    private String queryMyTask(BaseEntity entity, HttpServletRequest request) {
        List<UserTask> myTaskList = userTaskService.queryMyTask(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        return JSON.toJSONString(myTaskList);
    }


    /**
     * 月度非标准任务
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryFMyTask",method = RequestMethod.GET)
    private String queryFMyTask(BaseEntity entity, HttpServletRequest request) {
        List<UserTask> myTaskList = userTaskService.queryFMyTask(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        return JSON.toJSONString(myTaskList);
    }


    /**
     * 完成非正式任务
     * @param userTask
     * @param request
     * @return
     */
    @RequestMapping(value = "/completeMyFTask",method = RequestMethod.POST)
    @ResponseBody
    private String completeMyFTask(UserTask userTask, HttpServletRequest request) {
        Integer code = -1;
        userTask.setApproval("1");
        if (userTaskService.completeMyFTask(userTask) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 我的月度任务完成情况
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/completeMyTaskDetail",method = RequestMethod.GET)
    private String completeMyTaskDetail(BaseEntity entity, HttpServletRequest request) {
       List<String> myTaskList = userTaskService.completeMyTaskDetail(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
       return JSON.toJSONString(myTaskList);
    }

    /**
     *本月置业顾问任务完成情况
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/completeMyTaskDetailE",method = RequestMethod.GET)
    private String completeMyTaskDetailE(BaseEntity entity, HttpServletRequest request) {
        String userId = request.getParameter("userId");
        List<String> myTaskList = userTaskService.completeMyTaskDetailE(Integer.valueOf(userId));
        return JSON.toJSONString(myTaskList);
    }


    /**
     * 获取置业顾问本月任务量
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/myTaskDetail",method = RequestMethod.GET)
    private String myTaskDetail(BaseEntity entity, HttpServletRequest request) {
        String userId = request.getParameter("userId");
        List<String> myTaskList = userTaskService.myTaskDetail(Integer.valueOf(userId));
        return JSON.toJSONString(myTaskList);
    }


    /**
     * 新增转发信息
     * @param forward
     * @param request
     */
    @RequestMapping(value = "/insertForward",method = RequestMethod.POST)
    @ResponseBody
    private void insertForward(Forward forward, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        forward.setForwardDate(sdf.format(new Date()));
        forward.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        userTaskService.insertForward(forward);
    }



}
